package com.business_clean.ui.adapter.home;

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.SizeUtils;
import com.business_clean.R;
import com.business_clean.app.App;
import com.business_clean.app.uitl.ActivityForwardUtil;
import com.business_clean.app.uitl.ToastUtil;
import com.business_clean.data.mode.workbench.HomeGridItem;
import com.business_clean.data.mode.workbench.HomeGridList;
import com.business_clean.ui.activity.BaseH5Activity;
import com.business_clean.ui.activity.address.AddressManagerActivity;
import com.business_clean.ui.activity.attendance.MyAttendanceActivity;
import com.business_clean.ui.activity.classes.ClassesMangerActivity;
import com.business_clean.ui.activity.custom.CommonContactManagementActivity;
import com.business_clean.ui.activity.custom.CustomManagerActivity;
import com.business_clean.ui.activity.depart.DepartActivity;
import com.business_clean.ui.activity.group.GroupManagerActivity;
import com.business_clean.ui.activity.me.MyCenterActivity;
import com.business_clean.ui.activity.project.ProjectManagerActivity;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.idlefish.flutterboost.FlutterBoost;
import com.idlefish.flutterboost.FlutterBoostRouteOptions;
import com.luck.picture.lib.decoration.GridSpacingItemDecoration;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class HomeCardAdapter extends BaseQuickAdapter<HomeGridList, BaseViewHolder> {

    public HomeCardAdapter() {
        super(R.layout.item_home_card);
    }

    @Override
    protected void convert(@NotNull BaseViewHolder holder, HomeGridList item) {
        holder.setText(R.id.tv_item_home_card_title, item.getName());
        TextView tvUser = holder.getView(R.id.tv_item_home_card_user);
        if (item.getName().equals("个人中心") && App.appViewModelInstance.getUserInfo().getValue() != null) {
            tvUser.setText("您好，" + App.appViewModelInstance.getUserInfo().getValue().getUser().getUser_name());
        } else {
            tvUser.setText("");
        }

        RecyclerView recyclerView = holder.getView(R.id.rv_item_home_card_title);
        initRecyclerView(recyclerView, item);
    }

    private void initRecyclerView(RecyclerView recyclerView, HomeGridList item) {
        HomeItemAdapter itemAdapter = new HomeItemAdapter();
        recyclerView.setAdapter(itemAdapter);
        recyclerView.setLayoutManager(new GridLayoutManager(recyclerView.getContext(), 4));
        itemAdapter.setList(item.getApplication_menu_list());
        itemAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull @NotNull BaseQuickAdapter<?, ?> adapter, @NonNull @NotNull View view, int position) {
                onGridItemClick(itemAdapter.getData().get(position));
            }
        });
    }


    /**
     * 确定是那种类型的   //应用地址类型：1-外部url 2-小程序 3-app内部应用 4-二级应用
     *
     * @param homeGridItem
     */
    private void onGridItemClick(HomeGridItem homeGridItem) {
        switch (homeGridItem.getApplication_address_type()) {
            case "1":
                Bundle bundle = new Bundle();
//                bundle.putString("url", homeGridItem.getIcon_url());
                ActivityForwardUtil.startActivity(BaseH5Activity.class, bundle);
                break;
//            case "2":
//                break;
            case "3":
                gotoNativePage(homeGridItem);
                break;
            default:
                ToastUtil.show("无效的点击");
                break;
        }
    }

    //应用内跳转
    private void gotoNativePage(HomeGridItem homeGridItem) {
        HashMap<String, Object> hashMap = new HashMap<>();
        switch (homeGridItem.getId()) {
            case "1"://小组
                ActivityForwardUtil.startActivity(GroupManagerActivity.class);
                break;
            case "2"://班次
                ActivityForwardUtil.startActivity(ClassesMangerActivity.class);
                break;
            case "3"://打卡地点
                ActivityForwardUtil.startActivity(AddressManagerActivity.class);
                break;
            case "4"://离职档案
                ActivityForwardUtil.startActivity(DepartActivity.class);
                break;
            case "5"://常用联系人
                ActivityForwardUtil.startActivity(CommonContactManagementActivity.class);
                break;
            case "6"://我的考勤
                ActivityForwardUtil.startActivity(MyAttendanceActivity.class);
                break;
            case "11"://客户管理
                ActivityForwardUtil.startActivity(CustomManagerActivity.class);
                break;
            case "12"://项目管理
                ActivityForwardUtil.startActivity(ProjectManagerActivity.class);
                break;
            case "16"://身份核验
                FlutterBoost.instance().open(new FlutterBoostRouteOptions.Builder()
                        .pageName("identityPage")
                        .arguments(hashMap)
                        .build());
                break;
            case "17"://信用查询
                FlutterBoost.instance().open(new FlutterBoostRouteOptions.Builder()
                        .pageName("creditInquiryPage")
                        .arguments(hashMap)
                        .build());
                break;
            case "18"://电子合同
                FlutterBoost.instance().open(new FlutterBoostRouteOptions.Builder()
                        .pageName("contractPage")
                        .arguments(hashMap)
                        .build());
                break;
            case "19"://保险
                FlutterBoost.instance().open(new FlutterBoostRouteOptions.Builder()
                        .pageName("insurePage")
                        .arguments(hashMap)
                        .build());
                break;
            case "20"://岗位管理
                FlutterBoost.instance().open(new FlutterBoostRouteOptions.Builder()
                        .pageName("workPostPage")
                        .arguments(hashMap)
                        .build());
                break;
            case "22"://用工规则
                FlutterBoost.instance().open(new FlutterBoostRouteOptions.Builder()
                        .pageName("workRulesPage")
                        .arguments(hashMap)
                        .build());
                break;
            case "23"://更多
                ActivityForwardUtil.startActivity(MyCenterActivity.class);
                break;
            default:
                ToastUtil.show("此功能请升级新版本后才能使用哦");
                break;
        }
    }
}
